package com.mwt.oes.controller;

import com.mwt.oes.domain.Question;
import com.mwt.oes.domain.Questions;
import com.mwt.oes.dto.StudentRecordDTO;
import com.mwt.oes.service.AnsRecordService;
import com.mwt.oes.service.QuestionsService;
import com.mwt.oes.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

 * @create: 2024-02-22 22:16
 **/
@CrossOrigin
@RestController
@RequestMapping("/api/question")
public class QuestionsController {

    @Autowired
    private AnsRecordService ansRecordService;

    @RequestMapping("studentRecord")
    public ServerResponse studentRecord(){

        List<StudentRecordDTO> list = ansRecordService.studentRecord();
        List<Map<String, Object>> transform = transform(list);
        return ServerResponse.createBySuccess("获取成功", transform);
    }


    public static List<Map<String, Object>> transform(List<StudentRecordDTO> list) {
        Map<String, String> labelMap = createLabelMap();
        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        for (StudentRecordDTO item : list) {
            String sno = item.getSno();
            if (!map.containsKey(sno)) {
                map.put(sno, new ArrayList<>());
            }
            map.get(sno).add(createEntry(item.getValue(), item.getTotal(), labelMap.get(item.getValue())));

            map.get(sno).sort(Comparator.comparing((Map<String, Object> m) -> {
                String value = (String) m.get("value");
                return value.replaceAll("[A-Z]", "0$0").replaceAll("[a-z]", "1$0");
            }));
        }

        List<Map<String, Object>> result = new ArrayList<>();
        for (Map.Entry<String, List<Map<String, Object>>> entry : map.entrySet()) {
            String sno = entry.getKey();
            String name = list.stream().filter(item -> item.getSno().equals(sno)).findFirst().get().getName();

            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("sno", sno);
            resultMap.put("name", name);
            resultMap.put("values", map.get(sno));
            result.add(resultMap);
        }

        return result;
    }

    private static Map<String, Object> createEntry(String value, int total, String label) {
        Map<String, Object> entry = new HashMap<>();
        entry.put("value", value);
        entry.put("total", total);
        entry.put("label", label);
        return entry;
    }

    static class CustomComparator implements Comparator<String> {
        public int compare(String s1, String s2) {
            return s1.compareToIgnoreCase(s2);
        }
    }

    private static Map<String, String> createLabelMap() {
        Map<String, String> labelMap = new HashMap<>();
        labelMap.put("A", "投注性");
        labelMap.put("B", "領導性");
        labelMap.put("C", "冒險性");
        labelMap.put("D", "迅速性");
        labelMap.put("E", "活動性");
        labelMap.put("F", "社交性");
        labelMap.put("G", "思考性");
        labelMap.put("H", "細緻性");
        labelMap.put("I", "條理性");
        labelMap.put("J", "穩定性");
        labelMap.put("a", "意志力");
        labelMap.put("b", "成就需求");
        labelMap.put("c", "統御需求");
        labelMap.put("d", "表現慾");
        labelMap.put("e", "合群性");
        labelMap.put("f", "親和需求");
        labelMap.put("g", "喜新性");
        labelMap.put("h", "批判性");
        labelMap.put("i", "崇拜性");
        labelMap.put("j", "受役性");
        return labelMap;
    }
}
